/**
 * 开发环境Webpack配置
 */
const baseConfig = require('./base');
const path = require('path');
// const webpack = require('webpack');
// 额外处理所需组件
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const multi = require('./multi.conf.js');
// 遍历得到入口文件
const tpls = [];
for (const [key, val] of Object.entries(multi)) {
  const { tpl, vendor, chunks = [] } = val;
  // 是否需要加载公共文件
  if (vendor) {
    chunks.unshift('vendor');
  }
  chunks.unshift(key);
  tpls.push(new HtmlWebpackPlugin({
    template: path.join(baseConfig.dir.sourcePath, tpl),
    filename: path.join(baseConfig.dir.view, tpl),
    minify: {
      removeCommets: true,
      collapseWhitespace: true,
    },
    inject: true,
    chunks,
  }));
}

const option = {
  output: {
    path: baseConfig.dir.distPath,
    publicPath: '/',
    filename: 'scripts/[name].[chunkhash:5].bundle.js', // 文件输出
  },
};
const plugins = [
  ...baseConfig.basePlugins,
  // 多页处理
  ...tpls,
  // 混淆代码
  /*new UglifyJsPlugin({
    compress: true
  }),*/
  new ExtractTextPlugin('styles/[name].[hash:5].css'),
];

const options = Object.assign(option, baseConfig.prod);
options.plugins = plugins;

module.exports = options;
